﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace WaiGaoQiaoWeb.Extensions
{
    /// <summary>
    /// 大于
    /// </summary>
    public class DoubleGreaterCompareAttribute : ValidationAttribute, IClientValidatable
    {
        public DoubleGreaterCompareAttribute(string otherPropertyName)
            : base("{0} 必须大于 {1} ！")
        {
            OtherPropertyName = otherPropertyName;
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(ErrorMessage, name, OtherPropertyName); // "{0} must be greater than {1}"
        }

        public static string FormatPropertyForClientValidation(string property)
        {
            if (property == null)
            {
                throw new ArgumentException("", "property");
            }
            return "*." + property;
        }



        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherPropertyName);
            var otherValue = Convert.ToDouble(otherPropertyInfo.GetValue(validationContext.ObjectInstance, null));
            var thisValue = Convert.ToDouble(value);
            if (thisValue < otherValue)
            {
                var message = FormatErrorMessage(validationContext.DisplayName);
                return new ValidationResult(message);
            }
            return null;
        }

        public string OtherPropertyName { get; set; }


        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            yield return new ModelClientValidationMaxRule(FormatErrorMessage(metadata.GetDisplayName()), FormatPropertyForClientValidation(OtherPropertyName));

        }
    }


    /// <summary>
    /// 小于
    /// </summary>
    public class DoubleLessCompareAttribute : ValidationAttribute
    {
        public DoubleLessCompareAttribute(string otherPropertyName)
            : base("{0} 必须小于 {1} ！")
        {
            OtherPropertyName = otherPropertyName;
        }

        public override string FormatErrorMessage(string name)
        {
            return String.Format(ErrorMessage, name, OtherPropertyName); // "{0} must be greater than {1}"
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var otherPropertyInfo = validationContext.ObjectType.GetProperty(OtherPropertyName);
            var otherValue = Convert.ToDouble(otherPropertyInfo.GetValue(validationContext.ObjectInstance, null));
            var thisValue = Convert.ToDouble(value);
            if (thisValue > otherValue)
            {
                var message = FormatErrorMessage(validationContext.DisplayName);
                return new ValidationResult(message);
            }
            return null;
        }

        public string OtherPropertyName { get; set; }
    }



    public class ModelClientValidationMaxRule : ModelClientValidationRule
    {
        public ModelClientValidationMaxRule(string errorMessage, object other)
        {
            ErrorMessage = errorMessage;
            ValidationType = "max";
            ValidationParameters["other"] = other;
        }
    }
}